package com.example.farmer.Activity;

import android.content.Intent;
import android.os.Bundle;
import android.text.InputType;
import android.view.View;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.Toast;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.example.farmer.Entity.User;
import com.example.farmer.Database.UserMessage;
import com.example.farmer.R;

public class SignActivity extends AppCompatActivity {
    private UserMessage message ;
    private EditText number_edittext ,password_edittext,name_edittext,email_edittext,paypassword_edittext;
    private RadioButton male_radio ,female_radio ;
    private Button resister_button ;
    private CheckBox password_toggle ;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.sign);
        name_edittext=findViewById(R.id.name_edittext);
        number_edittext=findViewById(R.id.number_edittext);
        password_edittext=findViewById(R.id.password_edittext);
        male_radio=findViewById(R.id.male_radio);
        female_radio=findViewById(R.id.female_radio);
        email_edittext=findViewById(R.id.email_edittext);
        paypassword_edittext=findViewById(R.id.paypassword_edittext);
        resister_button=findViewById(R.id.register_button);
        password_toggle = findViewById(R.id.password_toggle);
        message = new UserMessage(this) ;

        password_toggle.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                if (isChecked) {
                    // 显示密码
                    password_edittext.setInputType(InputType.TYPE_TEXT_VARIATION_VISIBLE_PASSWORD);

                } else {
                    // 隐藏密码
                    password_edittext.setInputType(InputType.TYPE_CLASS_TEXT | InputType.TYPE_TEXT_VARIATION_PASSWORD);
                }
                password_edittext.setSelection(password_edittext.length()); // 将光标置于文本末尾
            }
        });

    }

    public void sign(View view) {
        String userName = name_edittext.getText().toString().trim();
        String userNumber = number_edittext.getText().toString().trim();
        String password = password_edittext.getText().toString().trim();
        String sex = male_radio.isChecked() ? "男" : "女";
        String email = email_edittext.getText().toString().trim();
        String paypassword = paypassword_edittext.getText().toString().trim();

        // 输入校验
        if (userName.isEmpty() ||userNumber.isEmpty() || password.isEmpty() || email.isEmpty()) {
            Toast.makeText(this, "所有字段均为必填项", Toast.LENGTH_SHORT).show();
            return;
        }
        if (password.length() < 6 ||paypassword.length() < 6 ) {
            Toast.makeText(this, "密码至少需要6位字符", Toast.LENGTH_SHORT).show();
            return;
        }
        if (password.length()>16 ||paypassword.length() >16){
            Toast.makeText(this, "密码不能超过16位字符", Toast.LENGTH_SHORT).show();
            return;
        }
        if (male_radio.isChecked() == female_radio.isChecked()) {
            Toast.makeText(this, "请选择性别", Toast.LENGTH_SHORT).show();
            return;
        }
        password_toggle.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {

            }
        });

        User user = new User(userName,userNumber , password,sex,email,paypassword);
        long insert = message.insertUser(user);
        // 检查输入是否有效，然后添加学生
        if(insert != -1){
            Toast.makeText(this , "注册成功" , Toast.LENGTH_SHORT).show();
            Intent intent = new Intent(SignActivity.this , LoginActivity.class);
            startActivity(intent);
            finish();
        }else {
            Toast.makeText(this , "注册失败" , Toast.LENGTH_SHORT).show();
        }
    }

}
